package java;

public class Question8 {
    public static void main(String[] args) {

        System.out.println(myAtoi("0-1a00 with words"));

    }
    public static int myAtoi(String str) {
        if (str == null || str.length() < 1){
            return 0;
        }
        int length = str.length();
        int start = -1;
        int end = length;
        char startChar = '+';
        int res;
        for (int i = 0; i < length ; i ++){
            if (str.charAt(i) == '-' ){
                if (i + 1 < length && str.charAt(i + 1) <= '9' && str.charAt(i + 1) >= '0'){
                    startChar = '-';
                    start = i;
                    break;
                }else {
                    return 0;
                }
            }
            if (str.charAt(i) == '+'){
                if (i + 1 < length && str.charAt(i + 1) <= '9' && str.charAt(i + 1) >= '0'){
                    startChar = '+';
                    start = i;
                    break;
                }else {
                    return 0;
                }
            }
            if (str.charAt(i) <= '9' && str.charAt(i) >= '0'){
                startChar = ' ';
                start = i;
                break;
            }
            if (str.charAt(i) != ' ' && str.charAt(i) != '0' && str.charAt(i) != '-' && str.charAt(i) != '+'){
                return 0;
            }
        }

        if (start != -1){
            for (int i = start + 1; i < length ; i ++){
                if (str.charAt(i) > '9' || str.charAt(i) < '0'){
                    end = i;
                    break;
                }
            }
            if (startChar != ' '){
                for (int i = start + 1; i < end ; i++){
                    if (str.charAt(i) == '0'){
                        start ++;
                    }else {
                        break;
                    }
                }
                if (startChar == '-'){
                    res = parseToInt(startChar + str.substring(start + 1,end));
                }else {
                    res = parseToInt(str.substring(start + 1,end));
                }
            }else {
                for (int i = start ; i < end ; i++){
                    if (str.charAt(i) == '0'){
                        start ++;
                    }else {
                        break;
                    }
                }
                res = parseToInt(str.substring(start,end));
            }
        }else {
            return 0;
        }
        return res;
    }

    private static int parseToInt(String substring) {
        if (substring == null || substring.length() < 1 || substring.equals("-")){
            return 0;
        }
        String maxInt = String.valueOf(Integer.MAX_VALUE);
        String minInt = String.valueOf(Integer.MIN_VALUE);
        String temp = maxInt;
        boolean flag = true;
        if (substring.startsWith("-")){
            temp = minInt;
        }
        if (substring.length() < temp.length()){
            return Integer.parseInt(substring);
        }else if (substring.length() > temp.length()){
            if (temp.startsWith("-")){
                return Integer.MIN_VALUE;
            }else {
                return Integer.MAX_VALUE;
            }
        }
        for (int i = 0;i < substring.length(); i ++){
            if (temp.charAt(i) > substring.charAt(i)){
                flag = true;
                break;
            }
            if (temp.charAt(i) < substring.charAt(i)){
                flag = false;
                break;
            }
        }
        if (flag){
            return Integer.parseInt(substring);
        }else {
            if (temp.startsWith("-")){
                return Integer.MIN_VALUE;
            }else {
                return Integer.MAX_VALUE;
            }
        }
    }
}
